package common.util;


import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * FileName: ConvertUtils
 *
 * @Author: fangyuan
 * Date:     2019/5/5 18:10
 * Email:   15072747528@163.com
 * Description:
 */
public class ConvertUtils {

    public static Integer[] stringToInt(String[] arrs) {
        Integer[] ints = new Integer[arrs.length];
        for (int i = 0; i < arrs.length; i++) {
            ints[i] = Integer.parseInt(arrs[i]);
        }
        return ints;
    }

    public static Integer[] stringToInt(String arrs) {
        if (StringUtils.isEmpty(arrs)) {
            return null;
        }
        return stringToInt(arrs.split(","));
    }

    public static String[] stringToArray(String str) {
        return str.split(",");
    }

    public static String intArrayToString(Integer[] array) {
        if (ObjectUtils.isEmpty(array)) {
            return null;
        }
        String str = "";
        for (int i = 0; i < array.length; i++) {
            if (StringUtils.isEmpty(str)) {
                str += String.valueOf(array[i]);
            } else {
                str += "," + String.valueOf(array[i]);
            }
        }
        return str;
    }

    public static String arrayToString(Object[] array) {
        if (ObjectUtils.isEmpty(array) || array.length == 0) {
            return null;
        }
        String str = "";
        for (int i = 0; i < array.length; i++) {
            if (StringUtils.isEmpty(str)) {
                str += array[i].toString();
            } else {
                str += "," + array[i].toString();
            }
        }
        return str;
    }

    public static <T> T convertUnite(Class<T> tClass, Object... obj) {
        T t = null;
        try {
            t = tClass.newInstance();
            for (Object item : obj) {
                BeanUtils.copyProperties(item, t);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> List<T> convertUnite(Class<T> tClass, List... obj) {
        List<T> tList = null;
        try {
            tList = new ArrayList<>();
            for (List item : obj) {
                for (Object list : item) {
                    T t = tClass.newInstance();
                    BeanUtils.copyProperties(list, t);
                    tList.add(t);
                }
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tList;
    }


    /**
     * 第一个对象里的数据往第2个对象里填充
     *
     * @param request
     * @param entity
     */
    public static void requestAddEntity(Object request, Object entity) {
        BeanUtils.copyProperties(request, entity);
    }

    /**
     * create by: i2twmx@gmail.com
     * description: 拷贝非空字段
     * create time: 2018/12/17
     */

    public static void copyNotnullProperties(Object request, Object entity) {
        BeanUtils.copyProperties(request, entity, getNullPropertyNames(request));
    }

    public static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }


    /**
     * create by: i2twmx@gmail.com
     * description:批量填充数据（两个集合size相等,拷贝非空字段）
     * create time: 2018/11/30
     *
     * @return
     */

    public static void listToList(List<?> lsit, List<?> list2) {
        for (int i = 0; i < lsit.size(); i++) {
            requestAddEntity(lsit.get(i), list2.get(i));
        }
    }

    public static <T> List<List<T>> averageAssign(List<T> source, int n) {
        List<List<T>> result = new ArrayList<>();
        int remainder = source.size() % n;
        int number = source.size() / n;
        int offset = 0;
        for (int i = 0; i < n; i++) {
            List<T> value;
            if (remainder > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remainder--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }

    public static String getRange(Long var1, Long var2) {
        Long num = 9999999L;
        String str = new String();
        for (Long i = var1; i <= var2; i++) {
            if (var1 > num) {
                str += i + ",";
            } else {
                str += "0" + i + ",";
            }

        }
        return str.substring(0, str.length() - 1);
    }

    public static List<String> getUnit(String field) {
        if (StrUtil.isNotBlank(field)) {
            String[] split = ReUtil.replaceAll(field, "(\\[[^\\]]*\\])", "").trim().split(";");
            if (split.length > 0) {
                return Arrays.asList(ArrayUtil.removeEmpty(split));
            }
            return Collections.emptyList();
        }
        return Collections.emptyList();
    }

    public static void main(String[] args) {
        System.out.println(getUnit("[de Medeiros, Paulo] Univ Warwick, Humanities Bldg,Univ Rd, Coventry CV4 7AL, W Midlands, England"));

    }
}
